/*
 * Amaze - QT downloader for the Amazon MP3 store
 * Copyright (c) 2010-2011 Robert Keevil
 *
 * This program is free software: you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <QDesktopServices>
#include <QDir>
#include "config.h"

Config::Config()
{
    settings = new QSettings("amaze");
    load_config();
}

Config::~Config()
{
    save_config();
}

void Config::load_config()
{
    settings->beginGroup("settings");

    automatic_download = settings->value("automatic_download", false).toBool();
    automatic_close = settings->value("automatic_close", false).toBool();
    output_folder  = settings->value("output_folder", default_folder()).toString();
    output_format = settings->value("output_format", AMAZE_DEFAULT_FORMAT).toString();

    settings->endGroup();
}

void Config::save_config()
{
    settings->beginGroup("settings");
    settings->setValue("automatic_download", automatic_download);
    settings->setValue("automatic_close", automatic_close);
    settings->setValue("output_folder", output_folder);
    settings->setValue("output_format", output_format);
    settings->endGroup();
    settings->sync();
}

/* try to find a sensible place to save downloads to */
QString Config::default_folder()
{
    QString home = QDir::homePath();
    QString ret = QDesktopServices::storageLocation(QDesktopServices::MusicLocation);

    if (ret != home && !ret.isEmpty())
        return ret;

    ret = QDesktopServices::storageLocation(QDesktopServices::DocumentsLocation);
    if (ret != home && !ret.isEmpty())
        return ret;
    else
        return home;
}
